package com.zcz.umengpush.util;

import org.json.JSONObject;

import java.util.Arrays;
import java.util.HashSet;

public abstract class UmengNotification {
	// This JSONObject is used for constructing the whole request string. 
	/**
	 * 这个JSONObject用于构造整个请求字符串。
	 */
	protected final JSONObject rootJson = new JSONObject();
	
	
	// The app master secret  
	/**
	 * app的密钥
	 */
	protected String appMasterSecret;
	
	// Keys can be set in the root level   
	/**
	 * 键可以在根级别设置
	 */
	protected static final HashSet<String> ROOT_KEYS = new HashSet<String>(Arrays.asList(new String[]{
			"appkey", "timestamp", "type", "device_tokens", "alias", "alias_type", "file_id", 
			"filter", "production_mode", "feedback", "description", "thirdparty_id","mi_activity","mipush"}));
	
	// Keys can be set in the policy level  
	/**
	 * 可以在策略级别设置密钥
	 */
	protected static final HashSet<String> POLICY_KEYS = new HashSet<String>(Arrays.asList(new String[]{
			"start_time", "expire_time", "max_send_num"
	}));
	
	/**
	 * 在rootJson中设置预定义的键，用于额外的键(Android)或定制的键(IOS)
	 * 引用子类中相应的方法。
	 * @param key
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public abstract boolean setPredefinedKeyValue(String key, Object value) throws Exception;
	/**
	 * 设置app 密钥
	 * @param secret
	 */
	public void setAppMasterSecret(String secret) {
		appMasterSecret = secret;
	}
	/**
	 * 获取主体
	 * @return
	 */
	public String getPostBody(){
		return rootJson.toString();
	}
	/**
	 * 获取app的密钥
	 * @return
	 */
	protected final String getAppMasterSecret(){
		return appMasterSecret;
	}
	/**
	 * 设置生产模式
	 * @param prod
	 * @throws Exception
	 */
	protected void setProductionMode(Boolean prod) throws Exception {
    	setPredefinedKeyValue("production_mode", prod.toString());
    }

	/**
	 * 正式模式
	 * @throws Exception
	 */
    public void setProductionMode() throws Exception {
    	setProductionMode(true);
    }

    /**
                * 测试模式
     * @throws Exception
     */
    public void setTestMode() throws Exception {
    	setProductionMode(false);
    }

    /**
     * 发送消息描述，建议填写
     * @param description
     * @throws Exception
     */
    public void setDescription(String description) throws Exception {
    	setPredefinedKeyValue("description", description);
    }
    
    /**
     * 定时发送时间，若不填写表示立即发送。格式: "YYYY-MM-DD hh:mm:ss"。
     * @param startTime
     * @throws Exception
     */
    public void setStartTime(String startTime) throws Exception {
    	setPredefinedKeyValue("start_time", startTime);
    }
    /**
     * 消息过期时间,格式: "YYYY-MM-DD hh:mm:ss"。
     * @param expireTime
     * @throws Exception
     */
    public void setExpireTime(String expireTime) throws Exception {
    	setPredefinedKeyValue("expire_time", expireTime);
    }
    /**
     * 发送限速，每秒发送的最大条数。
     * @param num
     * @throws Exception
     */
    public void setMaxSendNum(Integer num) throws Exception {
    	setPredefinedKeyValue("max_send_num", num);
    }

	/**
	 * 厂商通道包名
	 * @param mi_activity
	 * @param baoMing
	 * @throws Exception
	 */
	public void setxiaomi(String mi_activity,String baoMing) throws Exception {
		setPredefinedKeyValue(mi_activity, baoMing);
	}
	/**
	 * 是否使用厂商通道
	 * @param
	 * @param xiaomi
	 * @throws Exception
	 */
	public void setxiaomitrue(Boolean xiaomi) throws Exception {
		setxiaomiProductionMode(xiaomi);
	}
	/**
	 *是否使用厂商通道
	 * @param prod
	 * @throws Exception
	 */
	protected void setxiaomiProductionMode(Boolean prod) throws Exception {
		setPredefinedKeyValue("mipush", prod.toString());
	}

}
